package com.booking.springrestful.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booking.springrestful.domain.Approvaltable;
import com.booking.springrestful.domain.Counselors;
import com.booking.springrestful.domain.Users;
import com.booking.springrestful.domain.dto.ApprovaltableDto;
import com.booking.springrestful.mapper.ApprovaltableMapper;
import com.booking.springrestful.mapper.CounselorsMapper;
import com.booking.springrestful.mapper.UsersMapper;
import com.booking.springrestful.service.ApprovaltableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
* @author 13182
* @description 针对表【approvaltable(审批表，用于记录审批流程相关信息)】的数据库操作Service实现
* @createDate 2024-11-15 14:39:35
*/
@Service
@Slf4j
public class ApprovaltableServiceImpl extends ServiceImpl<ApprovaltableMapper, Approvaltable>
    implements ApprovaltableService {

    @Resource
    private ApprovaltableMapper approvalTableMapper;

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private CounselorsMapper counselorsMapper;


    //提交
    @Override
    public void submit(String username) {
        Approvaltable approvalTable = new Approvaltable();
        approvalTable.setApprovastatus("待审批");
        approvalTable.setInitiator(username);
        approvalTableMapper.insert(approvalTable);
    }

    @Override
    public void approval(ApprovaltableDto approvaltableDto) {
        String username = approvaltableDto.getUsername();
        Approvaltable approvaltable = new Approvaltable();
        BeanUtils.copyProperties(approvaltableDto, approvaltable);
        approvaltable.setApprover(approvaltableDto.getApprover());
        approvaltable.setApprovastatus("已审批");
        approvalTableMapper.update(approvaltable,new QueryWrapper<Approvaltable>().eq("initiator",username));

        log.info(approvaltableDto.getApprovalresult());
        //修改状态
        if (Objects.equals(approvaltableDto.getApprovalresult(), "同意")){
            Users users = new Users();
            users.setStatus("1");
            usersMapper.update(users,new QueryWrapper<Users>().eq("username",username));
            Counselors counselors = new Counselors();
            counselors.setStatus("1");
            Integer userid = usersMapper.selectOne(new QueryWrapper<Users>().eq("username", username)).getUserid();
            counselorsMapper.update(counselors,new QueryWrapper<Counselors>().eq("userId",userid));
        }else {
            Users users = new Users();
            users.setStatus("3");
            usersMapper.update(users,new QueryWrapper<Users>().eq("username",username));
        }

    }

    @Override
    public String getNote(String username) {
        Approvaltable approvaltable = approvalTableMapper.selectOne(new QueryWrapper<Approvaltable>().eq("initiator", username));
        return approvaltable.getApprovalnote();
    }

    @Override
    public void again(String userName) {
        Approvaltable approvalTable = new Approvaltable();
        approvalTable.setApprovastatus("待审批");
        approvalTable.setApprover(null);
        approvalTable.setApprovalnote(null);
        approvalTable.setApprovalresult(null);
        approvalTableMapper.update(approvalTable,new QueryWrapper<Approvaltable>().eq("initiator",userName));
    }
}




